package com.niiwoo.civet.account.service.local.bill;/**
 * Created by DELL on 2017/11/25.
 */

import com.niiwoo.civet.account.dao.entity.BillDateReleated;
import com.niiwoo.civet.account.dao.entity.BillDateReleatedExample;
import com.niiwoo.civet.account.dao.entity.NiiwooBillGuarantee;
import com.niiwoo.civet.account.dao.entity.NiiwooBillPlatform;
import com.niiwoo.civet.account.dao.mapper.BillDateReleatedMapper;
import com.niiwoo.civet.account.enums.BillDateReleatedTypeEnum;
import com.niiwoo.civet.account.enums.BillUserTypeEnum;
import com.niiwoo.tripod.service.component.SnowflakeIdWorker;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 关联订单日期的其它条件
 *
 * @author xiaoyingping
 * @create 2017-11-25 18:45
 **/
@Service
@Slf4j
public class BillDateReleatedService {

    @Autowired
    private BillDateReleatedMapper billDateReleatedMapper;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;


    /**
     * 新增担保机构的BillDateReleated
     * @param niiwooBillGuarantee
     */
    public void addBillDateReleated(NiiwooBillGuarantee niiwooBillGuarantee){
        String billDate = FastDateFormat.getInstance("yyyy-MM-dd").format(niiwooBillGuarantee.getCreateTime());
        this.addBillDateReleatedCommon(BillUserTypeEnum.GUARANTEE_AGENCY, BillDateReleatedTypeEnum.ID, niiwooBillGuarantee.getBillId().toString(), billDate);
        if(Objects.nonNull(niiwooBillGuarantee.getOrderId())){
            this.addBillDateReleatedCommon(BillUserTypeEnum.GUARANTEE_AGENCY, BillDateReleatedTypeEnum.ORDER_ID, niiwooBillGuarantee.getOrderId(), billDate);
        }
        if(Objects.nonNull(niiwooBillGuarantee.getProjectId())){
            this.addBillDateReleatedCommon(BillUserTypeEnum.GUARANTEE_AGENCY, BillDateReleatedTypeEnum.PROJECT_ID, String.valueOf(niiwooBillGuarantee.getProjectId()), billDate);
        }
    }


    /**
     * 新增平台的BillDateReleated
     * @param niiwooBillPlatform
     */
    public void addBillDateReleated(NiiwooBillPlatform niiwooBillPlatform){
        String billDate = FastDateFormat.getInstance("yyyy-MM-dd").format(niiwooBillPlatform.getCreateTime());
        this.addBillDateReleatedCommon(BillUserTypeEnum.GUARANTEE_AGENCY, BillDateReleatedTypeEnum.ID, niiwooBillPlatform.getBillId().toString(), billDate);
        if(Objects.nonNull(niiwooBillPlatform.getOrderId())){
            this.addBillDateReleatedCommon(BillUserTypeEnum.PLATFORM, BillDateReleatedTypeEnum.ORDER_ID, niiwooBillPlatform.getOrderId(), billDate);
        }
        if(Objects.nonNull(niiwooBillPlatform.getProjectId())){
            this.addBillDateReleatedCommon(BillUserTypeEnum.PLATFORM, BillDateReleatedTypeEnum.PROJECT_ID, String.valueOf(niiwooBillPlatform.getProjectId()), billDate);
        }
    }


    /**
     * 查询账单日期列表
     *
     * @param releatedTypeEnum
     * @param relatedValue
     * @return
     */
    public List<String> queryBillDates(BillDateReleatedTypeEnum releatedTypeEnum, String relatedValue) {
        Assert.notNull(releatedTypeEnum, "releatedTypeEnum不能为空");
        Assert.notNull(relatedValue, "relatedValue不能为空");

        BillDateReleatedExample example = new BillDateReleatedExample();
        BillDateReleatedExample.Criteria criteria = example.createCriteria();
        criteria.andRelatedTypeEqualTo(releatedTypeEnum.getValue());
        criteria.andRelatedValueEqualTo(relatedValue);

        List<BillDateReleated> billDateReleateds = billDateReleatedMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(billDateReleateds)) {
            log.error("{找不到对应的billDate, relatedValue:{}, releatedType:{}", relatedValue, releatedTypeEnum.getValue());
            return Collections.EMPTY_LIST;
        }

        List<String> dateList = billDateReleateds.stream().map(BillDateReleated::getBillDate).collect(Collectors.toList());
        return dateList;

    }


    private void addBillDateReleatedCommon(BillUserTypeEnum billUserType, BillDateReleatedTypeEnum releatedTypeEnum, String relatedValue, String billDate) {
        BillDateReleatedExample example = new BillDateReleatedExample();
        BillDateReleatedExample.Criteria criteria = example.createCriteria();
        criteria.andRelatedValueEqualTo(relatedValue);
        criteria.andRelatedTypeEqualTo(releatedTypeEnum.getValue());
        criteria.andBillDateEqualTo(billDate);
        long count = billDateReleatedMapper.countByExample(example);

        if(count == 0){
            BillDateReleated record = new BillDateReleated();
            record.setId(snowflakeIdWorker.nextId());
            record.setRelatedType(releatedTypeEnum.getValue());
            record.setRelatedValue(relatedValue);
            record.setBillDate(billDate);
            record.setBillSourceType(billUserType.getCode());
            record.setCreateTime(new Date());

            billDateReleatedMapper.insertSelective(record);
        }
    }




}
